use chrono::{DateTime, NaiveDate, Utc};
use git_tools::app;
use service::*;

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    app::bootstrap::init().await?;
    let date = NaiveDate::from_ymd_opt(2024, 1, 1)
        .unwrap()
        .and_hms_opt(0, 0, 0)
        .unwrap();
    tracing::info!("{:?}", date);

    let client =
        gitlab_api_service::new_gitlab_client("10.101.37.217", "xMyjD_gnvAm9tDdraRMy", false)
            .await?;

    let project_list = gitlab_info_service::list_project(1).await?;

    for project in project_list {
        let mut commit_service = gitlab_api_service::CommitService::new(
            &client,
            &project,
            Some(DateTime::<Utc>::from_naive_utc_and_offset(date, Utc)),
            None,
        )
        .await?;
        while let Some(commit_list) = commit_service.next_query().await? {
            tracing::info!(
                "{:?} - {:?} 查询提交数 {} 个",
                commit_service.current_after_date,
                commit_service.current_before_date,
                commit_list.len()
            );
            if !commit_list.is_empty() {
                gitlab_info_service::save_commits(&project, &commit_list).await?;
                tracing::info!(
                    "{} {}",
                    commit_list.first().unwrap().id,
                    commit_list.last().unwrap().id
                );
            }
        }
    }

    let proj = bean::Project {
        id: 1,
        name: "2".to_owned(),
        web_url: "3".to_owned(),
        created_at: chrono::Utc::now(),
    };

    println!("{:?}", serde_json::to_string(&proj));
    Ok(())
}
